<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>주소 선택하기</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
<script type="text/javascript" src="http://apis.daum.net/maps/maps2.js?apikey=fdedf555ead724e65234ad4eb1ccb6760c6a6f1a"></script>
<script type="text/javascript" src="./js/jquery-1.4.4.min.js"></script>
<style type="text/css">
<!--
	body { margin: 0; padding: 0; }
	map_canvas { width: 800px; height: 600px; }
-->
</style>
<script type="text/javascript">
	function setMapWith(address) {
		geocoder.geocode( {'address': address }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var validIndex;
				for (validIndex = 0; validIndex < results.length; validIndex++) {
					if (results[validIndex].geometry.location.lat() >=0 && results[validIndex].geometry.location.lng() >= 0) {
						//alert(results[validIndex].geometry.location.toString());
						break;
					}
				}
				if (validIndex >= results.length) {
					alert("검색결과가 없습니다");
					return;
				}
				var latlng = new DLatLng(results[validIndex].geometry.location.lat(), results[validIndex].geometry.location.lng());
				map.setCenter(latlng, 2);
			}
			else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
				alert("검색결과가 없습니다");
				return;
			}
			else
				alert("주소검색 중 네트워크 문제가 발생했습니다. 다시 입력해주세요");	
		});
	}
	function handleClickMap(point) {
		geocoder.geocode( {'latLng': new google.maps.LatLng(point.getLat(), point.getLng()) }, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
				var validIndex;
				for (validIndex = 0; validIndex < results.length; validIndex++) {
					if (results[validIndex].formatted_address) {
						//alert(results[validIndex].geometry.location.toString());
						break;
					}
				}
				if (validIndex >= results.length) {
					alert("검색결과가 없습니다");
					return;
				}
				//alert(results[validIndex].formatted_address);
				var address = results[validIndex].formatted_address;
				address = address.replace(/^대한민국 /, "");
				address = address.replace(/(특별시|광역시|북도|남도)/, "");
				latlng = "위도: " + point.getLat() + "\n경도: " + point.getLng();
				if (confirm(address + "\n" + latlng + "\n\n이 주소를 선택 하시겠습니까?")) {
					if (opener.gps_loc)
						opener.gps_loc.value = address;
					if (opener.latitude)
						opener.latitude.value = point.getLat();
					if (opener.longitude)
						opener.longitude.value = point.getLng();
					if (opener.gps_loc_detail)
						opener.gps_loc_detail.focus();
					window.close();
				}
			}
			else if (status == google.maps.GeocoderStatus.ZERO_RESULTS) {
				alert("검색결과가 없습니다");
				return;
			}
			else
				alert("주소검색 중 네트워크 문제가 발생했습니다. 다시 입력해주세요");	
		});
	}
	function addMarkers() {
		$.getJSON("get_location.php", function(data) {
			$.each(data.gps, function(gps, gps_info) {
				var m = new DMark(new DLatLng(gps_info.latitude, gps_info.longitude));
				DEvent.addListener(m, "click", handleClickMap);
				map.addOverlay(m);
				//$('#location').append("<option value='" + gps_info.latitude + "'>" + gps_info.longitude + "</option>");
			});
		});
	}
	
	$(document).ready(function() {
		var mapOption = {
			point: new DLatLng(37.5655342, 126.9384179),
			level: 3,
			map_type: "TYPE_SKYVIEW"
		};
		map = new DMap(document.getElementById("map_canvas"), mapOption);
		addMarkers();
		
		var zoomControl = new DZoomControl();
		var indexMapControl = new DIndexMapControl();
		map.addControl(zoomControl);
		zoomControl.setVAlign("middle");
		geocoder = new google.maps.Geocoder();
		document.getElementById("search_address").value = "신촌 연세대학교";
		DEvent.addListener(map, "click", handleClickMap);
	});
</script>
</head>
<body onload="initialize()">
	<div id="map_canvas"></div>
	<form id="map_search" action="#" onsubmit="setMapWith(this.search_address.value); return false;">
		<label for="search_address">지도검색: </label>
		<input type="text" id="search_address" name="search_address" size="80" />
		<input type="submit" value="검색" /><br />
	</form>	
</body>
</html>